// pages/myDevice/myDevice.js
import {
  $ajax
} from "../../utils/common.js";
import {

} from "../../utils/api.js";
let ajax = require("../../utils/ajax.js");
let user = require("../../utils/user.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    value: [0, 0],
    brandList: [],
    brandName: '未选择',
    serialName: '未选择',
  },
  bindChange(e) {
    const val = e.detail.value
    console.log(val);
    if (0 < val[0]) {

    }
    this.setData({
      index: val[0],
      val: [val[0], val[1]]
    })
  },
  save(e) {
    let vm = this;
    let brand_id = vm.data.brandList[vm.data.val[0]].id,
      serial_id = vm.data.brandList[vm.data.val[0]].child[vm.data.val[1]].id;
    ajax.requestPost(ajax.ajaxUrl() + 'member/save-model', {
      brand_id: brand_id,
      serial_id: serial_id,
    }, 'post', '', function(res) {
      if ('SUCCESS' === res.status) {
        wx.setStorageSync('member', res.data.member);
        vm.updateModel();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    user.isLogin();
    let vm = this;
    // 获取车型
    ajax.requestPost(ajax.ajaxUrl() + 'index/category', {}, 'post', '', function(res) {
      if ('SUCCESS' === res.status) {
        vm.setData({
          brandList: res.data
        })
        vm.updateModel();
      }
    });

  },
  // 更新车型信息
  updateModel: function() {
    let vm = this;
    let member = wx.getStorageSync('member');
    let brandList = vm.data.brandList;
    for (let i = 0; i < brandList.length; i++) {
      if (member.model_brand_id == brandList[i].id) {
        for (let j = 0; j < brandList[i].child.length; j++) {
          if (member.model_serial_id == brandList[i].child[j].id) {
            vm.setData({
              index: i,
              value: [i, j],
              brandName: brandList[i].name,
              serialName: brandList[i].child[j].name,
            })
            break;
          }
        }
        break;
      }
    }
  },

  getEvaluateList(id) {
    $ajax.get(get_Evaluate_List, {
      product_id: id,
    }).then(res => {

    })
  },
})